package com.ruoyi.SuveryInterface.SuveryUtils.util;

import org.apache.log4j.Logger;
import sun.misc.BASE64Decoder;

import javax.imageio.ImageIO;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Iterator;

public class PhotoServiceByImage {

    private static final Logger logger = Logger.getLogger(PhotoServiceByImage.class);

    public static void main(String[] args) {

        // base64转为jpg
    	String aa="/9j/4AAQSkZJRgABAgAAAQABAAD//gAKSFMwMQAuBgLwBQBr8gD/2wBDAA4KCgwKCQ4MCwwQDw4RFSMXFRMTFSseIBkjMiw1NDIsMTA4P1BEODtMPDAxRl9GTFNVWlpaNkNiaWFXaFBYWlb/2wBDAQ8QEBUSFSkXFylWOTE5VlZWVlZWVlZWVlZWVlZWVlZWVlZWVlZWVlZWVlZWVlZWVlZWVlZWVlZWVlZWVlZWVlb/wAARCAB2AGADASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb30PDc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rCu4n2Qwk7R6Ko7nsBXP+GbffqmoX8gA2osUZPYElm/kK6KOFnfz5clj9xSMbB/j60AeOePNDvPDupprWms0EVwxL+Wf9VJ6cdjWRe+K77WdHjsbwIzQyCUSgYJIBGD2717tqenW+p6fPY3ib4Jl2sO49CPQjrXgWu+H7vw3q01lcgtGw3Qy4wJFz1HuO47UAej2zN9mizn7g7+1FR2PnyWFs6ygBolIG0ccCigDr/L2uscRKs4JeUnL4GBwfyqdHjiZIUUgYOAOnFFFAFHWbYOYLoEB0Ow+4NM3nagGNzHAz0HGf6UUUANtAZYVIZli54z8zc9Sf8KlkhhWNtsSDIwcKORRRQA7RbIJbvIhVcSlgu0kZwOSM1oXGoLbnY6FnwCMcA/4d6KKAFgvPtUpjiBQAZ3Hk4+lcd8VYY4vDMD7cyfbEBc9eUf/AAFFFAFHSWB0exJ3/wDHvH/Gf7oooooA/9k=";
        PhotoServiceByImage.savePhotoByImageIO("D://zhb.jpg", aa);
//    	BASE64Decoder cc=new BASE64Decoder();
//		BASE64Encoder dd=new BASE64Encoder();
//		try {
//			String photo=dd.encode(cc.decodeBuffer(base64String_5));
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
    }

    /**
     * base64解码
     *
     * @param s
     * @return String 解码后的字节数组
     */
    public static byte[] getFromBASE64(String s) {

        BASE64Decoder decoder;
        if (s == null)
            return null;
        decoder = new BASE64Decoder();
        try {
            byte b[] = decoder.decodeBuffer(s);
            return b;
        } catch (IOException ie) {
            logger.error("****** 照片解码错误************", ie);
            return null;
        }
    }

    /**
     * 保存照片
     *
     * @param filename 照片文件名称
     * @param base64String 从接口获得照片的base64编码
     * @return
     */
    public static boolean savePhotoByImageIO(String filename,
                                             String base64String) {

        boolean f = false;
        try {
            Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpg");
            ImageWriter writer = writers.next();
            // 设备输出源
            File file = new File(filename);
            ImageOutputStream ios = ImageIO.createImageOutputStream(file);
            writer.setOutput(ios);

            // 读入输入流
            InputStream is = new ByteArrayInputStream(getFromBASE64(base64String));
            BufferedImage src = ImageIO.read(is);
            if (src != null) {
                // 输出
                writer.write(src);
            }
            return f;
        } catch (IOException ie) {
            logger.error("****** 保存照片错误************", ie);
            return f;
        } catch (Exception ex) {
            logger.error("****** 保存照片错误************", ex);
            return f;
        } finally {
        }
    }
    /**
     * @Description: 将base64编码字符串转换为图片
     * @Author:
     * @CreateTime:
     * @param imgStr base64编码字符串
     * @param path 图片路径-具体到文件
     * @return
    */
    public static boolean generateImage(String imgStr, String path) {
    	if (imgStr == null)
    			return false;
    BASE64Decoder decoder = new BASE64Decoder();
    try{
    // 解密
    	byte[] b = decoder.decodeBuffer(imgStr);
    // 处理数据
    for (int i = 0; i < b.length; ++i) {
   if (b[i] < 0) {
	   	b[i] += 256;
   		}
    }
    OutputStream out = new FileOutputStream(path);
    out.write(b);
    out.flush();
    out.close();
    return true;
   } catch (Exception e) {
	   return false;
   }
//
//    public static String getBase64(String filename) {
//
//        String base64 = null;
//        BASE64Encoder encoder = new BASE64Encoder();
//        try {
//            base64 = encoder.encodeBuffer(FileUtils.readFileToByteArray(new File(filename)));
//        } catch (IOException e) {
//            logger.error("****** 错误************", e);
//        }
//        return base64;
//    }
    }
}
